package com.controller.api;

import com.alibaba.fastjson.JSON;
import com.biz.*;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.cond.BaseCond;
import com.entity.model.*;
import com.entity.vo.BillListVO;
import com.entity.vo.CommissionVO;
import com.entity.vo.WithDrawVO;
import com.github.pagehelper.PageHelper;
import com.util.Base64;
import com.util.HttpRequest;
import com.util.PayUtil;
import com.util.UUIDHexGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.util.CalendarUtil.getAddDays2;

/**
 * Created by jc on 2019/11/16.
 */
@RestController("apiCommissionController")
@RequestMapping("/api/commission")
public class CommissionController {
    @Autowired
    private BillBiz billBiz;

    @Autowired
    private SysConfigBiz sysConfigBiz;

    @Autowired
    private CustomerBiz customerBiz;

    @Autowired
    private WithdrawBillBiz withdrawBillBiz;

    @RequestMapping(value = "/todayStatistics")
    public Result todayStatistics(@RequestParam Integer customerId) {
        CommissionVO commissionVO = new CommissionVO();

        BillListVO bill = billBiz.getTodayStatistics(customerId, 1);
        commissionVO.setTodayCommission(bill.getProfitFee());
        commissionVO.setTodayBillAmount(bill.getAmount());

        bill = billBiz.getTodayStatistics(customerId, 2);
        commissionVO.setHistoryCommission(bill.getProfitFee());

        return ResultGenerator.genSuccessResult(commissionVO);
    }


    @RequestMapping(value = "/withdrawStatistics")
    public Result withdrawStatistics(@RequestParam Integer customerId) {
        WithDrawVO withDrawVO = new WithDrawVO();

        BillListVO bill = billBiz.withdrawStatistics(customerId, 1);
        if (bill != null) {
            withDrawVO.setWithdraw(bill.getProfitFee());
        } else {
            withDrawVO.setWithdraw(0.00);
        }

        bill = billBiz.withdrawStatistics(customerId, 2);
        if (bill != null) {
            withDrawVO.setWithdrawing(bill.getProfitFee());
        } else {
            withDrawVO.setWithdrawing(0.00);
        }

        bill = billBiz.withdrawStatistics(customerId, 3);
        if (bill != null) {
            withDrawVO.setWithdrawed(bill.getProfitFee());
        } else {
            withDrawVO.setWithdrawed(0.00);
        }
        return ResultGenerator.genSuccessResult(withDrawVO);
    }


    @RequestMapping(value = "/goWithdraw")
    public Result goWithdraw(@RequestParam Integer customerId, @RequestParam Double withDrawFee) {
        SysConfig sysConfig = sysConfigBiz.findById(1);
        if (sysConfig != null) {
            Calendar c = Calendar.getInstance();
            int today = c.get(Calendar.DATE);

            if ((("," + sysConfig.getWithdrawalTime() + ",")).indexOf(("," + today + ",")) != -1) {
                Customer customer = customerBiz.findById(customerId);
                if (customer != null) {
                    if (customer.getBankName() == null || customer.getBankNo() == null) {
                        return ResultGenerator.genFailResult("请前往完善提现信息");
                    } else {
                        if (sysConfig.getVipPromotionalRewards() == 0) {
                            WithdrawBill withdrawBill = new WithdrawBill();

                            String nonce_str = UUIDHexGenerator.generate();//随机字符串
                            String day = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
                            String code = PayUtil.createCode(8);
                            String tx_no = "TX" + today + code;//提现订单号

                            withdrawBill.setBillNo(tx_no);
                            withdrawBill.setStatus(1);
                            withdrawBill.setCustomerId(customerId);
                            withdrawBill.setFee(withDrawFee);

                            withdrawBillBiz.save(withdrawBill);

                            billBiz.updateCommissionBill(customerId, withdrawBill.getId());
                        } else {
                            if (customer.getIsVip() < 2) {
                                return ResultGenerator.genFailResult("只有会员才能发起提现,请立即前往开通会员哟");
                            } else {
                                WithdrawBill withdrawBill = new WithdrawBill();

                                String nonce_str = UUIDHexGenerator.generate();//随机字符串
                                String day = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
                                String code = PayUtil.createCode(8);
                                String tx_no = "TX" + today + code;//提现订单号

                                withdrawBill.setBillNo(tx_no);
                                withdrawBill.setStatus(1);
                                withdrawBill.setCustomerId(customerId);
                                withdrawBill.setFee(withDrawFee);
                                withdrawBillBiz.save(withdrawBill);

                                billBiz.updateCommissionBill(customerId, withdrawBill.getId());
                            }
                        }
                    }
                } else {
                    return ResultGenerator.genFailResult("当前用户不存在");
                }
            } else {
                return ResultGenerator.genFailResult("今天不是提现日,请在下个提现日进行提现");
            }
        } else {
            return ResultGenerator.genFailResult("系统错误");
        }
        return ResultGenerator.genSuccessResult();
    }


    @RequestMapping(value = "/withdrawBill")
    public Result withdrawBill(BaseCond bcond, @RequestParam Integer customerId) {
        PageHelper.startPage(bcond.getPageNum(), bcond.getPageSize(), false);
        Condition condition = new Condition(WithdrawBill.class);
        condition.createCriteria().andEqualTo("customerId", customerId);
        List<WithdrawBill> withdrawBills = withdrawBillBiz.findByCondition(condition);
        return ResultGenerator.genSuccessResult(withdrawBills);
    }
}
